package com.example.java8.feature_06;

import java.util.function.BinaryOperator;
import java.util.stream.Stream;

public class TestReduce {


    public static void main(String[] args){

        // 累加计算
        Integer result = Stream.of(1, 2, 3, 4, 5).reduce((a, b) -> a + b).get();
        System.out.println("累加后的值:" + result);

        // 不使用lambda表达式
        /*Stream.of(1, 2, 3, 4, 5).reduce(new BinaryOperator<Integer>() {
            @Override
            public Integer apply(Integer a, Integer b) {
                return a + b;
            }
        }).get();*/

        Integer value = Stream.of(1, 2, 3, 4, 5).reduce(100, (a, b) -> a + b);
        System.out.println("value:" + value);


        // 求最大值
        Integer max = Stream.of(100, 99, 456, 12, 789).reduce((a, b) -> a > b ? a : b).get();
        System.out.println("最大值:" + max);
    }

}
